﻿using Payroll.BusinessLogic.Caching;
using Payroll.BusinessLogic.Data;
using Payroll.Common;
using Payroll.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Payroll.BusinessLogic.Devices
{
    public partial class VoucherService : IVoucherService
    {
        
        #region Fields

        /// <summary>
        /// Object context
        /// </summary>
        private readonly PayrollHRDataContext m_context;

        /// <summary>
        /// Cache manager
        /// </summary>
        private readonly ICacheManager _cacheManager;
       
        #endregion
        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="context">Object context</param>
        public VoucherService(PayrollHRDataContext context)
        {
            this.m_context = context;
            this._cacheManager = new PayrollRequestCache();
        }

        #endregion
        public int Save(Voucher voucher)
        {
            var item = (from d in m_context.Vouchers
                        where d.VoucherID == voucher.VoucherID
                        select d).FirstOrDefault();
            if (!CommonHelper.IsNull(item))
            {
                item.Transdate = voucher.Transdate;
                item.CustomerId = voucher.CustomerId;
                item.Address = voucher.Address;
                item.PacketId = voucher.PacketId;
                item.OwnerId = voucher.OwnerId;
                item.Vendor = voucher.Vendor;
                item.Note = voucher.Note; 
                item.Modified = voucher.Modified;
                item.ModifiedDate = DateTime.Now;
                item.Taxcode = voucher.Taxcode;
                item.CompanyName = voucher.CompanyName;
                item.Status = CommonHelper.ToInteger(voucher.Status);
            }
            else
            {
                item = new Data.Voucher
                {
                    Transdate = voucher.Transdate,
                    CustomerId = voucher.CustomerId,
                    Address = voucher.Address,
                    PacketId = voucher.PacketId,
                    OwnerId = voucher.OwnerId,
                    Vendor = voucher.Vendor,
                    Note = voucher.Note,
                    Createby = voucher.Createby,
                    Modified = voucher.Modified,
                    CreateDate = DateTime.Now,
                    ModifiedDate = DateTime.Now,
                    Taxcode = voucher.Taxcode,
                    CompanyName = voucher.CompanyName,
                    Status = CommonHelper.ToInteger(voucher.Status)
                };
                m_context.Vouchers.InsertOnSubmit(item);
            }
            m_context.SubmitChanges();
            return item.VoucherID;
        }

        public void Save(List<Voucher> List)
        {
            m_context.Vouchers.InsertAllOnSubmit(this.TranferOrderDevice(List));
            m_context.SubmitChanges();
        }

        public void Delete(string voucherid)
        {
            List<int> List = voucherid.Split(',').Select(Int32.Parse).ToList();
            var query = from c in m_context.Vouchers
                        where (from id in List select id).Contains(c.VoucherID)
                        select c;
            m_context.Vouchers.DeleteAllOnSubmit(query);
            m_context.SubmitChanges();
        }
        private List<Data.Voucher> TranferOrderDevice(List<Voucher> List)
        {
            List<Data.Voucher> Lst = new List<Data.Voucher>();
            foreach (var voucher in List)
            {
                Lst.Add(new Data.Voucher
                {
                    Transdate = voucher.Transdate,
                    CustomerId = voucher.CustomerId,
                    Address = voucher.Address,
                    PacketId = voucher.PacketId,
                    OwnerId = voucher.OwnerId,
                    Vendor = voucher.Vendor,
                    Note = voucher.Note,
                    Createby = voucher.Createby,
                    Modified = voucher.Modified,
                    CreateDate = DateTime.Now,
                    ModifiedDate = DateTime.Now,
                    Taxcode = voucher.Taxcode,
                    CompanyName = voucher.CompanyName
                });
            }
            return Lst;
        }


        public PagedList<Voucher> GetAllVoucher(DateTime? From, DateTime? To, string taxcode, string companyName, string vendor, int? voucherid, int? status, int pageSize, int pageIndex)
        {
            var query = from v in m_context.Vouchers
                        join c in m_context.Customers on v.CustomerId equals c.CustomerId into CustJoin
                        from c2 in CustJoin.DefaultIfEmpty()
                        join c3 in m_context.Customers on v.OwnerId equals c3.CustomerId into OwnerJoin
                        from c4 in OwnerJoin.DefaultIfEmpty()
                        join p in m_context.Packages on v.PacketId equals p.PackagesID into PacketJoin
                        from p2 in PacketJoin.DefaultIfEmpty()
                        where (v.VoucherID == voucherid || voucherid == null) && (v.Status == status || status == -1)
                        orderby v.VoucherID descending
                        select new Voucher
                        {
                            VoucherID = v.VoucherID,
                            Transdate = v.Transdate,
                            Taxcode = v.Taxcode ?? "",
                            CustomerId = v.CustomerId,
                            Address = v.Address ?? "",
                            PacketId = v.PacketId,
                            OwnerId = v.OwnerId,
                            Vendor = v.Vendor ?? "",
                            Note = v.Note ?? "",
                            CompanyName = v.CompanyName ?? "",
                            PacketName = p2.Name ?? "",
                            CustName = c2.CustName ?? "",
                            OwnerName = c4.CustName ?? "",
                            Status = v.Status == 0 ? "Đang xử lý" : "Hoàn thành"
                        };
            if (!CommonHelper.IsBlank(vendor))
            {
                query = query.Where(v => v.Vendor.ToLower().Equals(vendor.ToLower()));
            }
            if (!CommonHelper.IsNull(From))
            {
                query = query.Where(o => Convert.ToDateTime(o.Transdate) >= From && Convert.ToDateTime(o.Transdate) <= To);
            }
          
            return new PagedList<Voucher>(query, pageIndex, pageSize);
        }

        public Voucher GetVoucher(int voucherid)
        {
            return (from v in m_context.Vouchers
                    join c in m_context.Customers on v.CustomerId equals c.CustomerId into CustJoin
                    from c2 in CustJoin.DefaultIfEmpty()
                    join p in m_context.Packages on v.PacketId equals p.PackagesID into PacketJoin
                    from p2 in PacketJoin.DefaultIfEmpty()
                    where v.VoucherID == voucherid
                    select new Voucher
                    {
                        VoucherID = v.VoucherID,
                        Transdate = v.Transdate,
                        Taxcode = v.Taxcode ?? "",
                        CustomerId = v.CustomerId,
                        Address = v.Address ?? "",
                        PacketId = v.PacketId,
                        OwnerId = v.OwnerId,
                        Vendor = v.Vendor ,
                        Note = v.Note ?? "",
                        CompanyName = v.CompanyName ?? "",
                        PacketName = p2.Name ?? "",
                        Status = v.Status == 0 ? "Đang xử lý" : "Hoàn thành"
                    }).FirstOrDefault();
        }


        public void StatusUpdate(List<int> List)
        {
            (from v in m_context.Vouchers
                         where List.Contains(v.VoucherID)
                         select v).ToList().ForEach(v => v.Status = 1);
            m_context.SubmitChanges();
        }
    }
}
